﻿using B.MESUI.Login.A_Public.Constants;
using B.MESUI.Login.A_Public.Helpers;
using B.MESUI.Login.ProdManagement.DTO;
using B.MESUI.Login.ProdManagement.WorkOrder;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace B.MESUI.Login.ProdManagement.Lock
{
    public partial class ProductLockForm : DevExpress.XtraEditors.XtraForm
    {

        public delegate void ClearDataDelegate(bool topmost);
        private event ClearDataDelegate startcleart;


        public ProductLockForm()
        {
            InitializeComponent();
            button1.Click += button1_Click;
        }

        // API返回结构
        public class ApiResult
        {
            public int code { get; set; }
            public string message { get; set; }
            public WorkOrderListItem resultData { get; set; }
        }

     
        private async void button1_Click(object sender, EventArgs e)
        {
            if (CODEID.Text == "") 
            {
                MessageBox.Show("请输入工单编号！");
                return;
            }
            GetWorkOrderEntity();
            
        }


        public  async void GetWorkOrderEntity()
        {
            string codeId = CODEID.Text.Trim();
            string workOrderType = PRODUCE_TYPE.Text.Trim();

            var endpoint = ApiEndpoints.ProductLock.GetWorkOrderEntity;
            var url = HttpClientHelper.GetProductLockUrl(endpoint);
            var parameters = new Dictionary<string, object>
            {
                { "CODEID", codeId },
                { "WORKORDER_TYPE", workOrderType }
            };
            var queryString = HttpClientHelper.BuildQueryString(parameters);
            if (!string.IsNullOrEmpty(queryString))
                url += "?" + queryString;
            try
            {
                var response = await HttpClientHelper.GetAsync(url);
                var apiResult = JsonConvert.DeserializeObject<ApiResult>(response);

                if (apiResult.code == 200 && apiResult.resultData != null)
                {
                    var list = new List<WorkOrderListItem> { apiResult.resultData };
                    gridControl1.DataSource = list;
                }
                else
                {
                    gridControl1.DataSource = null;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("请求异常：" + ex.Message);
                gridControl1.DataSource = null;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string codeid = CODEID.Text.Trim();
            string workorderType = PRODUCE_TYPE.Text.Trim();
            LockDialogForm lockDialogForm = new LockDialogForm(codeid, workorderType);
            
            lockDialogForm.ClearDataEvent += () => {
                GetWorkOrderEntity();
            };
            lockDialogForm.ShowDialog();
        }

       
        // 新增：清空 CODEID 和下方列表的方法
        public void ClearData()
        {
            CODEID.Text = string.Empty;
            gridControl1.DataSource = null;
        }

        private void ProductLockForm_Load(object sender, EventArgs e)
        {
            
        }
    }
}